<template>
	<view>
		<zy-title hideBack :rightText="loginTypeTitle" @right="loginTypeChange"></zy-title>

		<view class="zy-ph-50">
			<view class="zy-text-white-lg-super-bold zy-mt-100">你好，</view>
			<view class="zy-text-white-lg-super-bold">{{loginTitle}}</view>
			
			<zy-input class="zy-mt-50" label='+86' hint='请输入手机号' v-model="account"></zy-input>
			<zy-input hint='请输入验证码' v-model="code" v-if="2==loginType">
				<zy-button-code :account='account'></zy-button-code>
			</zy-input>
			<zy-input hint='请输入密码' v-model="password" password v-if="1==loginType">
				<view class="zy-text-whitegrey">忘记密码？</view>
			</zy-input>
			
			<zy-button class="zy-mt-100" content="立即登录" @click="requestLogin"></zy-button>
			<zy-privacy ref="privacy" style="margin-top: 20rpx;"></zy-privacy>
		</view>

	</view>
</template>

<script>
	import LoginUtil from '@/main/common/LoginUtil.js'
	import RouteUtil from '@/main/common/RouteUtil.js'
	export default {
		data() {
			return {
				loginType: '2',
				loginTitle: '请填写手机号',
				loginTypeTitle: '密码登录',
				account: '',
				password: '',
				code: '708090',
			}
		},
		methods: {
			loginTypeChange() {
				if (1 == this.loginType) {
					this.loginType = 2;
					// this.loginTitle = '手机号登录';
					this.loginTypeTitle = '密码登录';
				} else {
					this.loginType = 1;
					// this.loginTitle = '密码登录';
					this.loginTypeTitle = '验证码登录';
				}
			},
			goRegister() {
				RouteUtil.toRegister()
			},
			requestLogin() {
				if (!this.$refs.privacy.value()) {
					uni.$toast.showToast('请勾选隐私政策')
					return
				}
				let param = {
					loginName: this.account
				};
				if (1 == this.loginType) {
					param['passWord'] = this.password;
				} else {
					param['smsCode'] = this.code;
				}

				uni.$http.post('/api/login', param)
					.then(res => {
						LoginUtil.saveToken(res.data)
						uni.$http.get('/api/getLoginUser')
							.then(res => {
								LoginUtil.login(res, this.loginType)
							})
					})
			},
		}
	}
</script>

<style lang="scss" scoped>
</style>